﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class bj_BLL
    {
        //创建数据对象
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 获取班级数据源
        /// </summary>
        public static object GetBJSource()
        {
            object obj = new object();
            obj = from p in db.BJ orderby p.zyNum,p.bjName,p.zgId select new { p.bjNum, p.ZY.YX.yxName, p.ZY.zyName, p.bjName, p.ZG.zgName, p.bjZF };
            return obj;
        }

        /// <summary>
        /// 获取班级数据源
        /// </summary>
        /// <param name="zgId">职工ID</param>
        public static object GetBJSource(string zgId)
        {
            object obj = new object();
            obj = from p in db.BJ orderby p.zyNum,p.bjName,p.zgId where p.zgId == decimal.Parse(zgId) select new { p.bjNum, p.ZY.YX.yxName, p.ZY.zyName, p.bjName, p.ZG.zgName, p.bjZF };
            return obj;
        }

        /// <summary>
        /// 获取班级数据源
        /// </summary>
        /// <param name="zgId">辅导员编号</param>
        /// <param name="zyNum">专业编号</param>
        public static object GetBJSource(string zgId, string zyNum)
        {
            object obj = new object();
            obj = from p in db.BJ orderby p.zyNum where p.zgId == decimal.Parse(zgId) && p.zyNum == decimal.Parse(zyNum) select new { p.bjNum, p.ZY.YX.yxName, p.ZY.zyName, p.bjName, p.ZG.zgName, p.bjZF };
            return obj;
        }

        /// <summary>
        /// 添加新班级
        /// </summary>
        /// <param name="newBJ">新班级数据对象</param>
        public static bool AddnewBJ(BJ newBJ)
        {
            bool bl = false;
            //切分多个班级
            string[] bjs = newBJ.bjName.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            //要添加的班级数据
            int n = bjs.Length;
            //添加成功标记
            int i = 0;
            //预添加数据对象
            BJ nBJ;
            foreach (string bj in bjs)
            {
                //实例化数据对象
                nBJ = new BJ();
                try
                {
                    //相同专业是否存在同名班级
                    if (db.BJ.Count(p => p.bjName == bj && p.zyNum == newBJ.zyNum) > 0)
                    {
                        continue;
                    }
                }
                catch { }
                //组装数据
                nBJ.bjName = bj;
                nBJ.bjZF = newBJ.bjZF;
                nBJ.zgId = newBJ.zgId;
                nBJ.zyNum = newBJ.zyNum;
                //准备更新
                db.BJ.InsertOnSubmit(nBJ);
                //提交更新
                db.SubmitChanges();
                //记录添加成功条数
                i++;
            }
            if (i != 0)
                bl = true;
            return bl;
        }
        /// <summary>
        /// 删除班级
        /// </summary>
        /// <param name="bjNum">要删除的班级的编号</param>
        public static bool DeleteBJ(string bjNum)
        {
            bool bl = false;
            db = new dbExamDataContext();
            try
            {
                BJ bj = db.BJ.Single(p => p.bjNum == decimal.Parse(bjNum));
                db.BJ.DeleteOnSubmit(bj);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 更新班级
        /// </summary>
        /// <param name="bjNum">要更新的班级编号</param>
        /// <param name="newBJ">新的数据内容</param>
        public static bool UpdateBJ(string bjNum, BJ newBJ)
        {
            bool bl = false;
            try
            {
                BJ bj = db.BJ.Single(p => p.bjNum == decimal.Parse(bjNum));
                bj.bjName = newBJ.bjName;
                bj.bjZF = newBJ.bjZF;
                bj.zgId = newBJ.zgId;
                bj.zyNum = newBJ.zyNum;
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
